package cpm.fsp.fragmentmanagerdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
* 主界面
*/

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Fragment[] mFragments;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private ImageView tab1;
    private ImageView tab2;
    private ImageView tab3;
    private TextView tab1_tv;
    private TextView tab2_tv;
    private TextView tab3_tv;
    private RelativeLayout learning_layout1;
    private RelativeLayout learning_layout2;
    private RelativeLayout learning_layout3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        tab1 = (ImageView) findViewById(R.id.tab1);
        tab2 = (ImageView) findViewById(R.id.tab2);
        tab3 = (ImageView) findViewById(R.id.tab3);

        tab1_tv = (TextView) findViewById(R.id.tab1_tv);
        tab2_tv = (TextView) findViewById(R.id.tab2_tv);
        tab3_tv = (TextView) findViewById(R.id.tab3_tv);

        learning_layout1 = (RelativeLayout) findViewById(R.id.learning_layout1);
        learning_layout2 = (RelativeLayout) findViewById(R.id.learning_layout2);
        learning_layout3 = (RelativeLayout) findViewById(R.id.learning_layout3);

        tab1.setSelected(true);

        mFragments = new Fragment[3];
        fragmentManager = getSupportFragmentManager();
        mFragments[0] = fragmentManager.findFragmentById(R.id.maintab_fragment0);
        mFragments[1] = fragmentManager.findFragmentById(R.id.maintab_fragment1);
        mFragments[2] = fragmentManager.findFragmentById(R.id.maintab_fragment2);

        fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);

        fragmentTransaction.show(mFragments[0]).commit();

        learning_layout1.setOnClickListener(this);
        learning_layout2.setOnClickListener(this);
        learning_layout3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
        switch (v.getId()){
            case R.id.learning_layout1:
                tab1.setSelected(true);
                tab2.setSelected(false);
                tab3.setSelected(false);
                tab1_tv.setTextColor(this.getResources().getColor(R.color.text_blue));
                tab2_tv.setTextColor(this.getResources().getColor(R.color.text_grey));
                tab3_tv.setTextColor(this.getResources().getColor(R.color.text_grey));
                fragmentTransaction.show(mFragments[0]).commitAllowingStateLoss();
                break;
            case R.id.learning_layout2:
                tab2.setSelected(true);
                tab1.setSelected(false);
                tab3.setSelected(false);
                tab2_tv.setTextColor(this.getResources().getColor(R.color.text_blue));
                tab1_tv.setTextColor(this.getResources().getColor(R.color.text_grey));
                tab3_tv.setTextColor(this.getResources().getColor(R.color.text_grey));
                fragmentTransaction.show(mFragments[1]).commitAllowingStateLoss();
                break;
            case R.id.learning_layout3:
                tab3.setSelected(true);
                tab2.setSelected(false);
                tab1.setSelected(false);
                tab3_tv.setTextColor(this.getResources().getColor(R.color.text_blue));
                tab2_tv.setTextColor(this.getResources().getColor(R.color.text_grey));
                tab1_tv.setTextColor(this.getResources().getColor(R.color.text_grey));
                fragmentTransaction.show(mFragments[2]).commitAllowingStateLoss();
                break;
        }
    }
}
